<template>
  <view class="container">
 
    <u-form :model="form" ref="uForm" labelWidth="100px" :rules="rules">
     
      <u-form-item label="提醒标题" prop="title" required>
        <u--input  v-model="form.title"  placeholder="请填写提醒标题"  />
      </u-form-item>
	  <u-form-item label="提醒形式" required>
	    <u-radio-group v-model="form.type">
	  		  <u-radio name="follow">跟进</u-radio>
	      <u-radio name="repayment">还款</u-radio>
	      <u-radio name="birthday">生日</u-radio>
	    </u-radio-group>
	  </u-form-item>
      <u-form-item label="提醒内容" prop="content" required>
        <u--textarea  v-model="form.content"  placeholder="请填写提醒内容"  />
      </u-form-item>
      <u-form-item label="提醒时间" required @click="showDateTime = true; hideKeyboard()">
		<u--input
			v-model="form.trigger_time"
			disabled
			disabledColor="#ffffff"
			placeholder="请选择提醒时间"
			border="none"
		></u--input>
	</u-form-item>
      <u-button type="primary" @click="submit">提交</u-button>
    </u-form>
	<u-datetime-picker
		v-model="value1"
		:show="showDateTime"
		mode="datetime"
		closeOnClickOverlay
		@confirm="dateConfirm"
		@close="showDateTime=false"
	></u-datetime-picker>
  </view>
</template>

<script>
	import {
		store,
		mutations
	} from '@/uni_modules/uni-id-pages/common/store.js'
export default {
  data() {
    return {
      form: {
        customer_id: '',
        type: 'follow',
		title:'',
        content: '',
        trigger_time: null,
		is_completed:false
      },
	  value1:Number(new Date()),
	  showCalendar: false,
	  disabled1: false,
      showDateTime: false,
      rules: {
		trigger_time: [{ required: true, message: '请选择时间', trigger: 'change' }],
        content: [{ required: true, message: '请填写内容', trigger: 'blur' }]
      }
    }
  },
  computed: {
   	  userInfo() {
   	  	return store.userInfo
   	  }
  },
  onLoad(e) {
    this.form.customer_id = e.customerId
  },
  onReady() {
  	this.$refs.uForm.setRules(this.rules)
  },
  methods: {
    async submit() {
      this.$refs.uForm.validate().then(res => {
          this.saveFollowRecord()
      }).catch(errors => {
		uni.$u.toast('校验失败')
	  })
    },
    dateConfirm(e) {
		this.showDateTime = false
		const timeFormat = uni.$u.timeFormat
		this.form.trigger_time =timeFormat(e.value)
	},
	hideKeyboard() {
		uni.hideKeyboard()
	},
    async saveFollowRecord() {
      const db = uniCloud.database()
      const data = {
        ...this.form,
		user_id: this.userInfo._id
      }
      await db.collection('mydb-remind').add(data)
      uni.showToast({ title: '保存成功' })
      setTimeout(() => uni.navigateBack(), 1500)
    }
  }
}
</script>